package com.yzf.wisecoder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.yzf.wisecoder.wisecodercommon.response.Page;
import com.yzf.wisecoder.wisecodercommon.response.Response;
import com.yzf.wisecoder.wisecodercommon.utils.BeanCopyUtils;
import com.yzf.wisecoder.wisecodercommon.utils.SecurityUtils;
import com.yzf.wisecoder.wisecodercommon.utils.ValidatorUtils;
import com.yzf.wisecoder.wisecodercommon.vo.request.ProjectReq;
import com.yzf.wisecoder.wisecodercommon.vo.response.ProjectResp;
import com.yzf.wisecoder.wisecoderservice.service.ProjectService;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.ProjectEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author jianghuiyong
 * @email jianghuiyong@yunzhangfang.com
 * @date 2020-05-06 17:43:12
 */
@RestController
@RequestMapping("/sys/project")
@AllArgsConstructor
@Api(value = "项目管理控制", tags = "项目管理控制")
public class ProjectController extends BaseController {

    private final ProjectService projectService;

    /**
     * 获取项目分页
     *
     * @param params
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "获取项目分页")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "projectName", name = "项目名称(用户模糊查询)", dataType = "String")
    })
    public Response<Page<ProjectResp>> page(@RequestParam Map<String, Object> params) {
        IPage<ProjectEntity> page = projectService.queryPage(params);
        Page<ProjectResp> entityPage = new Page<>();
        entityPage.setCurrent(page.getCurrent()).setPageSize(page.getSize())
                .setTotal(page.getTotal()).setTotalPage(page.getPages())
                .setList(BeanCopyUtils.copyList(page.getRecords(), ProjectResp.class));

        return Response.buildSuccessResponseWithData(entityPage);
    }

    /**
     * 获取项目列表
     *
     * @param params
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(value = "projectName", name = "项目名称(用户模糊查询)", dataType = "String")
    })
    @GetMapping("/list")
    @ApiOperation(value = "获取项目列表")
    public Response<List<ProjectResp>> list(@RequestParam Map<String, Object> params) {
        List<ProjectEntity> list = projectService.queryList(params);
        List<ProjectResp> projectResps = BeanCopyUtils.copyList(list, ProjectResp.class);
        projectResps.forEach(projectResp -> {
            projectResp.setIsOwner(projectResp.getCreator().longValue() == SecurityUtils.getUserId().longValue());

        });
        return Response.buildSuccessResponseWithData(projectResps);
    }

    /**
     * 新增或修改项目信息
     *
     * @param projectReq
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "新增或修改项目信息")
    public Response saveOrUpdate(@RequestBody ProjectReq projectReq) {
        ValidatorUtils.validateEntity(projectReq);
        ProjectEntity projectEntity = new ProjectEntity();
        BeanUtils.copyProperties(projectReq, projectEntity);
        projectService.saveOrUpdateProject(projectEntity);
        return Response.buildSuccessResponse();
    }

    /**
     * @param projectId
     * @return
     */
    @ApiOperation(value = "删除项目以及项目用户关系信息")
    @DeleteMapping("remove")
    public Response remove(@RequestParam Long projectId) {
        projectService.deleteByIds(Lists.newArrayList(projectId));

        return Response.buildSuccessResponse();
    }

}
